<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'
import {
  NAlert,
  NButton,
  NForm,
  NFormItem,
  NInput,
  NInputNumber,
  NPopconfirm,
  NSelect,
  NSwitch,
  NTag,
  NTreeSelect
} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'
import TheIcon from '@/components/icon/TheIcon.vue'

import { renderIcon } from '@/utils'
import { useCRUD } from '@/composables'
// import { loginTypeMap, loginTypeOptions } from '@/constant/data'
import api from '@/api'

defineOptions({ name: '定时任务规则' })

const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')

const {
  modalVisible,
  modalTitle,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: '定时任务规则',
  initForm: { order: 0 },
  doCreate: api.createJobsLog,
  doUpdate: api.updateJobsLog,
  doDelete: api.deleteJobsLog,
  refresh: () => $table.value?.handlerefresh(),
})

const deptOption = ref([])
const isDisabled = ref(false)

onMounted(() => {
  $table.value?.handleSearch()
  api.getJobsRuleList().then((res) => (deptOption.value = res.data))
})

const deptRules = {
  name: [
    {
      required: true,
      message: '请输入分类名称',
      trigger: ['input', 'blur', 'change'],
    },
  ],
}

async function addDepts() {
  isDisabled.value = false
  handleAdd()
}

const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 80,
    align: 'center',
    ellipsis: { tooltip: true },
  },
    {
    title: '任务名称',
    key: 'job_name',
    width: 120,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '执行状态',
    key: 'result_status',
    align: 'center',
    width: 100,
    ellipsis: { tooltip: true },
    render(row) {
      return h(
          NTag,
          { type: 'primary', size: 'small', bordered: false, style: { margin: '2px 3px' } },
          { default: () => row.result_status }
      )
    },
  },
     {
    title: '执行结果',
    key: 'result_data',
    align: 'center',
    width: 'auto',
    // ellipsis: { tooltip: true },
    render(row) {
      return h(
          NAlert,
          { type: 'success', size: 'small', bordered: false, style: { margin: '2px 3px' } },
          { default: () => row.result_data }
      )
    },
  },
     {
    title: '执行时间',
    key: 'created_at',
    width: '200',
    align: 'center',
    ellipsis: { tooltip: true },
  },


]

function formatTimestamp(timestamp) {
  const date = new Date(timestamp)
  const pad = (num) => num.toString().padStart(2, '0')
  const year = date.getFullYear()
  const month = pad(date.getMonth() + 1) // 月份从0开始，所以需要+1
  const day = pad(date.getDate())
  const hours = pad(date.getHours())
  const minutes = pad(date.getMinutes())
  const seconds = pad(date.getSeconds())
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
const datetimeRange = ref()
const handleDateRangeChange = (value) => {
  queryItems.value.start_time = formatTimestamp(value[0])
  queryItems.value.end_time = formatTimestamp(value[1])
}
</script>

<template>
  <!-- 业务页面 -->
  <CommonPage show-footer title="执行日志">
    <template #action>
      <div>

      </div>
    </template>
    <!-- 表格 -->
    <CrudTable
        ref="$table"
        v-model:query-items="queryItems"
        :columns="columns"
        :get-data="api.getJobsLog"
    >
      <template #queryBar>
        <QueryBarItem label="任务选择" :label-width="80">
          <NSelect
              v-model:value="queryItems.job_id"
              style="width: 150px"
              :options="deptOption"
              clearable
              placeholder="请选任务"
          />
        </QueryBarItem>
        <QueryBarItem label="执行时间" :label-width="70">
              <NDatePicker
                v-model:value="datetimeRange"
                type="datetimerange"
                clearable
                placeholder="请选择时间范围"
                @update:value="handleDateRangeChange"
              />
            </QueryBarItem>
      </template>
    </CrudTable>

  </CommonPage>
</template>
